package com.ibeetl.admin.core.web;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ibeetl.admin.core.entity.CoreOrg;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.rbac.UserLoginInfo;
import com.ibeetl.admin.core.rbac.tree.MenuItem;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.service.CoreUserService;
import com.ibeetl.admin.core.util.HttpRequestLocal;
import com.ibeetl.admin.core.util.PlatformException;

@Controller
public class IndexController {

	@Autowired
	CorePlatformService platformService;

	@Autowired
	CoreUserService userService;

	@Autowired
	HttpRequestLocal httpRequestLocal;

	@RequestMapping("/")
	public ModelAndView login() {
		ModelAndView view = new ModelAndView("/login.html");
		return view;
	}

	@RequestMapping("/login.do")
	@ResponseBody
	public JsonResult login(String code, String password) {
		JsonResult jr = new JsonResult();
		UserLoginInfo info = userService.login(code, password);
		if (info == null) {
			jr.setMsg("用户名密码错");
			jr.setCode("400");
			return jr;
		}
		if(info.getCode().equals("400")){
			jr.setMsg(info.getMsg());
			jr.setCode("400");
			return jr;
		}
		CoreUser user = info.getUser();
		CoreOrg currentOrg = info.getOrgs().get(0);
		for (CoreOrg org : info.getOrgs()) {
			if (org.getId() == user.getOrgId()) {
				currentOrg = org;
				break;
			}
		}
		info.setCurrentOrg(currentOrg);
		// 记录登录信息到session
		this.platformService.setLoginUser(info.getUser(), info.getCurrentOrg(), info.getOrgs());

		jr.setMsg("登录成功！");
		jr.setCode("0");
		return jr;
	}

//	@PostMapping("/login.do")
//	public ModelAndView login(String code, String password) {
//		if(StringUtils.isEmpty(code)){
//			throw new PlatformException("用户名不能为空");
//		}
//		if(StringUtils.isEmpty(password)){
//			throw new PlatformException("密码不能为空");
//		}
//		UserLoginInfo info = userService.login(code, password);
//		if (info == null) {
//			throw new PlatformException("用户名密码错");
//		}
//		CoreUser user = info.getUser();
//		CoreOrg currentOrg = info.getOrgs().get(0);
//		for (CoreOrg org : info.getOrgs()) {
//			if (org.getId() == user.getOrgId()) {
//				currentOrg = org;
//				break;
//			}
//		}
//
//		info.setCurrentOrg(currentOrg);
//		// 记录登录信息到session
//		this.platformService.setLoginUser(info.getUser(), info.getCurrentOrg(), info.getOrgs());
//		ModelAndView view = new ModelAndView("redirect:/index.do");
//		return view;
//	}
	
	@RequestMapping("/index.do")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView("/index.html");
		CoreUser currentUser = platformService.getCurrentUser();
		Long orgId = platformService.getCurrentOrgId();
		MenuItem menuItem = platformService.getMenuItem(currentUser.getId(), orgId);
		view.addObject("menus", menuItem);
		return view;
	}

	@RequestMapping("/logout.do")
	public ModelAndView logout(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Enumeration eum = session.getAttributeNames();
		while(eum.hasMoreElements()) {
			String key = (String)eum.nextElement();
			session.removeAttribute(key);
		}
		ModelAndView view = new ModelAndView("redirect:/");
		return view;
	}
	@RequestMapping("/changeOrg.do")
	public ModelAndView changeOrg(HttpServletRequest request,Long orgId) {
		platformService.changeOrg(orgId);
		ModelAndView view = new ModelAndView("redirect:/index.do");
		return view;
	}

	@RequestMapping("/getNotice.do")
	public ModelAndView getNotice(){
		ModelAndView view = new ModelAndView("/notice.html");
		CoreUser currentUser = platformService.getCurrentUser();
		return view;
	}

	@PostMapping("/forgetPassword.do")
	public ModelAndView forgetPassword(){
		ModelAndView view = new ModelAndView("/forgetPassword.html");

		return view;
	}
}
